import { getuser, putuser } from "../../utils/api"
import '../../utils/lodash_fix'
let _ = require("../../utils/lodash.min");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: {}
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({ user: wx.getStorageSync('user') })
  },
  xghead() {
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      success: res => {
        console.log(res)
        let head = res.tempFiles[0].tempFilePath
        let timestamp = (new Date()).valueOf()
        wx.cloud.uploadFile({
          cloudPath: 'user_head_img/' + timestamp + '.png',
          filePath: head,
          success: res => {
            console.log(res)
            wx.showToast({ title: '上传成功' })
            putuser({ head_img_url: res.fileID }).then(res => {
              if (_.isEqual(res.statusCode, 200)) {
                this.setData({ user: res.data.data })
                wx.setStorageSync('user', res.data.data)
              }
            })
          },
          fail: err => {
            console.log(err)
            wx.showToast({ title: '上传失败', icon: 'error' });
          }
        })
      }
    })
  },
  putxgname() {
    wx.showModal({
      title: '修改昵称',
      editable: true,
      placeholderText: '请输入新的昵称',
      success: res => {
        if (res.confirm) {
          if (res.content != '') {
            putuser({ nick_name: res.content }).then(res => {
              if (_.isEqual(res.statusCode, 200)) {
                wx.showToast({
                  title: '修改成功'
                })
                this.setData({ user: res.data.data })
                wx.setStorageSync('user', res.data.data)
              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none'
                })
              }
            })
          }
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})